﻿using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Web;
using Types.CustomTypes;

namespace Contracts.ClientTools
{
    public class CustomClientMessageInspector : IClientMessageInspector
    {
        #region IClientMessageInspector Members

        private CContextMetaData ContextMetaData
        {
            get
            {
                if (HttpContext.Current != null && HttpContext.Current.Session != null && HttpContext.Current.Session["CONTEXTDATA"] != null)
                    return (CContextMetaData)HttpContext.Current.Session["CONTEXTDATA"];
                return null;
            }
        }

        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            return;
        }

        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            MessageBuffer buffer = request.CreateBufferedCopy(request.ToString().Length);
            Message tRequest = buffer.CreateMessage();

            if (ContextMetaData != null)
            {
                MessageHeader mHeader = MessageHeader.CreateHeader(ContextParameters.ContextMetaDataName, ContextParameters.ContextMetaDataNamespace, ContextMetaData);
                tRequest.Headers.Add(mHeader);
            }
            else
            {
                CContextMetaData mData = new CContextMetaData();
                int key = -1;

                if (HttpContext.Current != null && HttpContext.Current.Session != null && HttpContext.Current.Session["DepartmentKey"] != null)
                    int.TryParse(HttpContext.Current.Session["DepartmentKey"].ToString(), out key);
                mData.DepartmentKey = key;

                if (HttpContext.Current != null && HttpContext.Current.Session != null && HttpContext.Current.Session["PersonelKey"] != null)
                    int.TryParse(HttpContext.Current.Session["PersonelKey"].ToString(), out key);
                mData.PersonelKey = key;

                mData.IssName = string.Empty;
                if (HttpContext.Current != null && HttpContext.Current.Session != null && HttpContext.Current.Session["IssName"] != null)
                    mData.IssName = HttpContext.Current.Session["IssName"].ToString();

                mData.ClientInfo = string.Empty;
                if (HttpContext.Current != null && HttpContext.Current.Session != null && HttpContext.Current.Session["ClientInfo"] != null)
                    mData.ClientInfo = HttpContext.Current.Session["ClientInfo"].ToString();

                mData.PageReadOnly = string.Empty;
                if (HttpContext.Current != null && HttpContext.Current.Session != null && HttpContext.Current.Session["PageReadOnly"] != null)
                    mData.PageReadOnly = HttpContext.Current.Session["PageReadOnly"].ToString();

                mData.CurrenPageUrl = string.Empty;
                mData.CurrenPageUrl = HttpContext.Current == null ? string.Empty : HttpContext.Current.Request.ServerVariables["URL"].ToString();

                // ---------------------------------------------------------------------------------------------------------------------------------
                MessageHeader mHeader = MessageHeader.CreateHeader(ContextParameters.ContextMetaDataName, ContextParameters.ContextMetaDataNamespace, mData);
                tRequest.Headers.Add(mHeader);
            }

            request = tRequest;
            buffer.Close();
            return null;
        }

        #endregion

    }

    public class CustomClientMessageInspectorBehavior : IEndpointBehavior
    {
        #region IEndpointBehavior Members

        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            CustomClientMessageInspector inspector = new CustomClientMessageInspector();
            clientRuntime.MessageInspectors.Add(inspector);

        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }

        #endregion
    }
}
